// packageF/debt/debtor/debtorShop/debtorShop.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navInd: 0,
    navArr: [],
    shopShow: false,
    classInfoOne: [],
    userInfo: [],
    ClassTypeInfo: [],
    kid: null,
    shopListMember: [],
    shopListMemberLoadMore: false,
    shopListPgae: 1,
    // 1债权人 2债务人 3VIP
    member_type: 2,
    ThrMemberArr: [],
    debtUid: '',
    // 是否有权限观看
    jurisdiction: false,
    imgPath: app.globalData.imgPath, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '债务人店铺',
    })
    if (options.kid) {
      this.setData({
        kid: options.kid
      })
    }
    if (options.member_type) {
      this.setData({
        member_type: options.member_type
      })
    }
    if (options.debtUid) {
      this.setData({
        debtUid: options.debtUid
      })
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == "debtUid") {
            this.setData({
              debtUid: chil_arr[1]
            })
          }
        }
      }
    }
    this.getUser();
    this.getClassType();
    this.setData({
      plugin_setting: wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {}
    })
  },
  tapNav(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      navInd: key
    })
    if (key == 2) {
      this.getThrMember();
    }
  },
  goSwitchShop(e) {
    console.log(e)
    let id = e.currentTarget.dataset.item.debtor_id;
    wx.navigateTo({
      url: '/packageF/debt/debtor/debtorShop/debtorShop?kid=' + id + '&member_type=1',
    })
  },
  goSwitchShopHome() {
    wx.reLaunch({
      url: '/packageG/index/index',
    })
  },
  //   请求债权人我的数据
  getThrMember() {
    let json = {
      member_type: 1
    };
    if (this.data.userInfo.debtor_id) {
      json.debtor_id = this.data.userInfo.debtor_id;
    }
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.getDebtData');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            ThrMemberArr: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toClassSearch(e) {
    let id = e.currentTarget.dataset.item.id;
    if (id == 0 && e.currentTarget.dataset.item.name == '全部分类') {
      console.log('全部分类')
      // wx.navigateTo({
      //   url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?debtSearch=true',
      // });
      this.setData({
        navInd: 1
      })
      return
    }
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?debtCId=' + id + '&debtId=' + this.data.userInfo.debtor_id,
    });
  },
  toSearch(e) {
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierSearch/SupplierSearch?debtSearch=true' + '&debtId=' + this.data.userInfo.debtor_id,
    });
  },
  shopCloseTap() {
    this.setData({
      shopShow: false
    })
  },
  shopShowTap() {
    this.setData({
      shopShow: true
    })
    // 初始化
    this.SwitchoverListInit();
    this.getSwitchoverList()
  },
  sortOut(e) {
    console.log(e, "sortOut");
  },
  // 债权人我的跳转按钮
  toVIP() {
    wx.navigateTo({
      url: '/packageF/debt/vipAdd/vipAdd?debtId=' + this.data.userInfo.debtor_id,
    })
  },
  toMyorder() {
    wx.navigateTo({
      url: '/packageA/member/myOrder_v2/myOrder_v2?member_type=1&debtId=' + this.data.userInfo.debtor_id,
    })
  },
  toDetail() {
    wx.navigateTo({
      url: '/packageF/debt/purchaseDetails/purchaseDetails?coupon=true&debtId=' + this.data.userInfo.debtor_id,
    })
  },
  // 债权人我的跳转按钮 end
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.navInd == 2) {
        this.getThrMember();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.navInd == 0) {
      if (this.data.current_page >= this.data.good_total_page) {
        console.log('没有更多')
      } else {
        console.log('加载下一页')
        const sortZj = this.selectComponent("#sort");
        sortZj.setData({
          good_page:sortZj.data.good_page + 1
        })
        // 调用加载下一页的方法
        sortZj.nexTgetGoodsData();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let ispath = '';
    if (this.data.debtUid) {
      ispath = "/packageF/debt/debtor/debtorShop/debtorShop?debtUid=" + this.data.debtUid + "&mid=" + mid;
    } else if (this.data.kid) {
      ispath = "/packageF/debt/debtor/debtorShop/debtorShop?kid=" + this.data.kid + "&mid=" + mid;
    } else {
      ispath = "/packageF/debt/debtor/debtorShop/debtorShop?mid=" + mid;
    }
    return {
      path: ispath
    }
  },
  sortOut(e) {
    this.setData({
      current_page: e.detail.current_page,
      good_total_page: e.detail.good_total_page
    })
  },
  SwitchoverListInit() {
    this.setData({
      shopListPgae: 1,
      shopListMemberLoadMore: false,
      shopListMember: []
    })
  },
  // 获取店铺切换列表
  getSwitchoverList() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.owner.debtorList');
    app._getNetWork({
      url: urlStr,
      data: {
        is_use: 1,
        page: this.data.shopListPgae
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res, '获取店铺切换列表')

          if (res.data.current_page < res.data.last_page) {
            this.setData({
              shopListMemberLoadMore: true
            })
          } else if (res.data.current_page >= res.data.last_page) {
            this.setData({
              shopListMemberLoadMore: false,
            })
          }
          if (this.data.shopListMemberLoadMore) {
            this.setData({
              shopListMember: this.data.shopListMember.concat(res.data.data)
            })
          } else {
            this.setData({
              shopListMember: res.data.data
            })
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getUser() {
    let json = {};
    console.log(this.data.debtor_uid,'aaaaaaaaaa')
    if (this.data.debtUid) {
      json = {
        debtor_uid: this.data.debtUid
      }
    } else {
      json = {
        debtor_id: this.data.kid ? this.data.kid : ''
      }
    }
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.getDebtor');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            userInfo: res.data,
            member_type: res.data.member_type,
            jurisdiction:true
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          this.setData({
            jurisdiction: false
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getClassType() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.goods.recommendCategory');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data.length)
          if (res.data.length <= 9) {
            res.data.push({
              id: 0,
              thumb_url: `${this.data.imgPath}debt/allClass.png`,
              name: '全部分类',
            })
          } else if (res.data.length >= 10) {
            let ajson = {
              id: 0,
              thumb_url: `${this.data.imgPath}debt/allClass.png`,
              name: '全部分类',
            }
            res.data.push(ajson)
            this.setData({
              ClassTypeInfo: res.data.slice(0, 10),
              navArr: res.data.slice(10)
            })
            return
          } else if (res.data.length == 19) {
            let ajson = {
              id: 0,
              thumb_url: `${this.data.imgPath}debt/allClass.png`,
              name: '全部分类',
            }
            res.data.push(ajson)
            this.setData({
              ClassTypeInfo: res.data.slice(0, 10),
              navArr: res.data.slice(10)
            })
            console.log(res.data)
            return
          }
          this.setData({
            ClassTypeInfo: res.data,
            navArr: []
          })
          console.log(this.data.ClassTypeInfo)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
})